package com.benchmark.springboot.controller;

import com.power.common.model.CommonResult;
import org.springframework.boot.availability.AvailabilityChangeEvent;
import org.springframework.boot.availability.ReadinessState;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * SpringBoot 2.3+就绪探针测试
 *
 * @author yu 2020/9/22.
 */
@RestController
public class ReadinessSettingController {

    @Resource
    private ApplicationEventPublisher eventPublisher;

    /**
     * 设置就绪探针为就绪状态
     * @return
     */
    @GetMapping("/readiness/accepting")
    public CommonResult<String> markReadinesAcceptingTraffic() {
        AvailabilityChangeEvent.publish(eventPublisher, this, ReadinessState.ACCEPTING_TRAFFIC);
        return CommonResult.ok().setResult("Readiness marked as ACCEPTING_TRAFFIC");
    }

    /**
     * 设置就绪探针为拒绝状态
     * @return
     */
    @GetMapping("/readiness/refuse")
    public CommonResult<String> markReadinesRefusingTraffic() {
        AvailabilityChangeEvent.publish(eventPublisher, this, ReadinessState.REFUSING_TRAFFIC);
        return CommonResult.ok().setResult("Readiness marked as REFUSING_TRAFFIC");
    }

}
